﻿using FOModels.LongTermLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.LongTermLoan
{
    public class LongTermLoanDAL
    {
         private SQLDataAccess db;
        public LongTermLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from LongTermLoan order by LongTermLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from LongTermLoan sa join LongTermLoanLang hl on sa.LongTermLoanID = hl.LongTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getLongTermLoanrelati(int CompanyID, int LongTermLoanID, int langid)
        {
            return db.exeSelect("select * from LongTermLoan ca join LongTermLoanLang hl on ca.LongTermLoanID = hl.LongTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.LongTermLoanID != " + LongTermLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getLongTermLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from LongTermLoan sa join LongTermLoanLang hl on sa.LongTermLoanID = hl.LongTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableLongTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getLongTermLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from LongTermLoan sa join LongTermLoanLang hl on sa.LongTermLoanID = hl.LongTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableLongTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getLongTermLoanDefault(int langid)
        {
            return db.exeSelect("Select * from LongTermLoan sa join LongTermLoanLang hl on sa.LongTermLoanID = hl.LongTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableLongTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getLongTermLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from LongTermLoan sa join LongTermLoanLang hl on sa.LongTermLoanID = hl.LongTermLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableLongTermLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getLongTermLoanByID(int LongTermLoanID, int langid)
        {
            return db.exeSelect("select * from LongTermLoan ca join LongTermLoanLang hl on ca.LongTermLoanID = hl.LongTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.LongTermLoanID = " + LongTermLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getLongTermLoanByIDManager(int LongTermLoanID, int langid)
        {
            return db.exeSelect("select * from LongTermLoan ca join LongTermLoanLang hl on ca.LongTermLoanID = hl.LongTermLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.LongTermLoanID = " + LongTermLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateLongTermLoan(LongTermLoanModels models)
        {
            return db.exeUpdate("update LongTermLoan set LongTermLoanName = N'" + models.LongTermLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where LongTermLoanID = " + models.LongTermLoanID + "");
        }
        public int InsertLongTermLoan(LongTermLoanModels models)
        {
            return db.exeUpdate("insert into LongTermLoan values (N'" + models.LongTermLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteLongTermLoan(int LongTermLoanID)
        {
            return db.exeUpdate("Delete from LongTermLoan where LongTermLoanID = " + LongTermLoanID + "");
        }
        public int UpdateLongTermLoanLang(LongTermLoanModels models, int langid)
        {
            return db.exeUpdate("update LongTermLoanLang set LongTermLoanName = N'" + models.LongTermLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where LongTermLoanID = " + models.LongTermLoanID + " and LangID=" + langid + "");
        }
        public int InsertLongTermLoanLang(LongTermLoanModels models, int langid)
        {
            return db.exeUpdate("insert into LongTermLoanLang values (" + models.LongTermLoanID + "," + langid + ",N'" + models.LongTermLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteLongTermLoanLang(int LongTermLoanID)
        {
            return db.exeUpdate("Delete from LongTermLoanLang where LongTermLoanID = " + LongTermLoanID + "");
        }

        public int DeleteLongTermLoanApply(int LongTermLoanApplyID)
        {
            return db.exeUpdate("Delete from LongTermLoanApply where LongTermLoanApplyID = " + LongTermLoanApplyID + "");
        }
        public DataTable getcontractbyid(int LongTermLoanapplyid)
        {
            return db.exeSelect("select * from LongTermLoanApply ca join [LongTermLoan] c on ca.LongTermLoanID = c.LongTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.LongTermLoanApplyID = " + LongTermLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from LongTermLoanApply ca join [LongTermLoan] c on ca.LongTermLoanID = c.LongTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from LongTermLoanApply ca join [LongTermLoan] c on ca.LongTermLoanID = c.LongTermLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int LongTermLoanApply(int LongTermLoanID, LongTermLoanApplyModels app)
        {
            return db.exeUpdate("insert into LongTermLoanApply(LongTermLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + LongTermLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(LongTermLoanApplyModels LongTermLoan)
        {
            return db.exeUpdate("update LongTermLoanApply set StatusID= " + LongTermLoan.StatusID + " where LongTermLoanApplyID = " + LongTermLoan.LongTermLoanApplyID + "");
        }
    }
}
